package com.qiangesoft.blog.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.blog.common.mvc.PageQuery;
import com.qiangesoft.blog.common.mvc.PageResultVO;
import com.qiangesoft.blog.system.entity.SysNotice;
import com.qiangesoft.blog.system.mapper.SysNoticeMapper;
import com.qiangesoft.blog.system.service.ISysNoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统公告 服务实现类
 * </p>
 *
 * @author qiange
 * @since 2023-06-28
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {

    @Override
    public PageResultVO<SysNotice> listNotice(PageQuery pageQuery, String title, Boolean isPublish) {
        PageResultVO<SysNotice> pageResultVO = new PageResultVO<>();
        Integer pageNum = pageQuery.getPageNum();
        Integer pageSize = pageQuery.getPageSize();
        pageResultVO.setPageNum(pageNum);
        pageResultVO.setPageSize(pageSize);

        LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(isPublish != null, SysNotice::getIsPublish, isPublish)
                .like(StringUtils.isNotBlank(title), SysNotice::getTitle, title);

        Page<SysNotice> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        pageResultVO.setTotal(page.getTotal());
        pageResultVO.setPages(page.getPages());
        pageResultVO.setResults(page.getRecords());
        return pageResultVO;
    }

    @Override
    public void insertNotice(SysNotice sysNotice) {
        baseMapper.insert(sysNotice);
    }

    @Override
    public void updateNotice(SysNotice sysNotice) {
        baseMapper.updateById(sysNotice);
    }

    @Override
    public void deleteNotice(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> list) {
        baseMapper.deleteBatchIds(list);
    }
}
